package main

import (
	_ "embed"
	"flag"
	"fmt"
	ccc "imageConvert/config"
	"imageConvert/pkg/mlog"
	"imageConvert/router"
	"os"

	"github.com/pelletier/go-toml/v2"
)

var (
	port = flag.Int("port", 0, "http port")
)

const (
	version = "1.0.0"
)

func main() {
	flag.Parse()
	fmt.Println(ccc.DefaultConfig)
	config := struct {
		HTTP struct {
			Port int `json:"port"`
		}
		Log mlog.LogConfig
	}{}
	if file, err := os.Open("config.toml"); err == nil {
		defer file.Close()
		if err = toml.NewDecoder(file).Decode(&config); err == nil {
			mlog.SetConfig(config.Log)
		}
	}

	var httpPort int = 3166
	if port != nil && *port > 0 {
		httpPort = *port
	} else {
		if config.HTTP.Port > 0 {
			httpPort = config.HTTP.Port
		}
	}
	mlog.Infof("图片转换工具服务启动... 端口:%v", httpPort)
	mlog.Infof("版本:%v", version)
	mlog.Infof("可打开浏览器，输入： http://localhost:%v 使用图片转换工具", httpPort)
	router.InitRouter(httpPort)
}
